package xyz.chaobei.feature.parallel;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @author <a href='mailto:maruichao52@gmail.com'>MRC</a>
 * @since 2021/2/18
 */
public class ParalleStream {

    public static void main(String[] args) {

        int max = 1000000;
        List<String> list = new ArrayList<>(max);
        List<String> list1 = new ArrayList<>(max);

        for (int i = 0; i < max; i++) {
            UUID uuid = UUID.randomUUID();
            list.add(uuid.toString());
            list1.add(uuid.toString());
        }

        // 进行串行排序
        list.stream().sorted().count();
        long start = System.nanoTime();
        long sorted = list1.parallelStream().sorted().count();
        System.out.println("串行排序，排序数量：" + sorted);
        long end = System.nanoTime();

        // 将纳秒转换为毫秒
        long millis = TimeUnit.NANOSECONDS.toMillis(end - start);
        System.out.println("串行排序，排序时间(ms)：" + millis);

        // 返回纳秒为单位的时间戳
        long start1 = System.nanoTime();
        long sorted1 = list1.parallelStream().sorted().count();
        System.out.println("并行排序，排序数量：" + sorted1);

        long end1 = System.nanoTime();
        long millis1 =  TimeUnit.NANOSECONDS.toMillis(end1 - start1);
        System.out.println("并行排序，排序时间(ms)：" + millis1);
    }

}
